package com.func;

import java.util.function.Function;

/**
 * https://zhuanlan.zhihu.com/p/357918015
 *
 * @author: chenhs
 * @date: Created in 10:58 2022/7/12
 **/
public class TestFunction {

    public static void main(String[] args) {

        //普通写法
        /*Function<Integer, Integer> f = new Function<Integer, Integer>() {
            @Override
            public Integer apply(Integer integer) {
                return integer + 1;
            }
        };*/

        //Lambda写法
        Function<Integer, Integer> f1 = i -> i + 1;
        Function<Integer, Integer> g1 = g -> g * 2;

        System.out.println(f1.apply(2));            //--------------(1)
        System.out.println(g1.apply(2));            //--------------(2)

        System.out.println(f1.andThen(g1).apply(3));    //--------------(3)
        System.out.println(f1.compose(g1).apply(3));    //--------------(4)

        System.out.println(Function.identity().compose(g1).apply(4));   //--------------(5)
    }
}
